<?php

require('config.php');
include('inc/functions.php');
include('inc/config.php');
include('inc/template.php');
include('inc/user.php');
include('inc/page.php');
include('inc/client.php');
include('inc/plan.php');
include('inc/contract.php');
include('lang/pt_BR.php');

$page = new page();
$config = new config();

$timestamp_now = date('YmdHis');
$timestamp_timeout = date('YmdHis', mktime(date(H)-1));

$arp = arp();

$contracts_sql = '
	SELECT `contracts`.`ip`, `contracts`.`mac`, `contracts`.`client_username`,
		`clients`.`last_access`, `clients`.`active`
	FROM `contracts`
	INNER JOIN `clients` ON (
		`contracts`.`client_username` = `clients`.`username` )
	WHERE `contracts`.`status` = "L"';
if (!$contracts_result = $db->query($contracts_sql)) die($db->error);

while ($contract = $contracts_result->fetch_object()) {

	foreach ($arp as $row) {

		if ($row['ip_address'] == $contract->ip && $row['hw_address'] == $contract->mac) {
			$contract->last_access = $timestamp_now;
			$timestamp_sql = '
				UPDATE `clients`
				SET `last_access` = "'.$timestamp_now.'"
				WHERE `username` = "'.$contract->client_username.'"';
			$db->query($timestamp_sql);
		}

	}

	if ($timestamp_timeout > $contract->last_access && $contract->active) {
		$query_disactivate = 'UPDATE `clients` SET `active` = 0 WHERE `username` = "'.$contract->client_username.'"';
		$db->query($query_disactivate);
		exec('echo "'.$timestamp_now.': Disactivating '.$contract->client_username.' (Inactivity)" >> /var/log/vulpes.log');
	}

}

$contracts_result->free();
iptables_refresh(false, false);

?>
